using System;
using System.Data.Linq;
using System.Linq;
using InfoTech.Core.Data.SqlClient;
using InfoTech.Core.Service;
using InfoTech.LanTranh.MucVu.Bussiness;

namespace InfoTech.LanTranh.MucVu.Service
{
    public class HOGIADINH_GIAODANService : BaseService
    {
        public static bool Check_Insert_HOGIADINH_GIAODAN(HOGIADINH_GIAODAN hogiadinhGiaodan)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<HOGIADINH>().FirstOrDefault(c => c.MAHOGIADINH == hogiadinhGiaodan.MAHOGIADINH) != null;
            //result &= dataContext.GetTable<HOGIADINH_GIAODAN>().FirstOrDefault(c => c.MAGIAODAN == hogiadinhGiaodan.MAGIAODAN) == null;
            return result;
        }

        public static bool Check_Delete_HOGIADINH_GIAODAN(string[] strings)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<HOGIADINH>().Join(dataContext.GetTable<HOGIADINH_GIAODAN>().Where(c => strings.Contains(c.MANHANKHAU + "")), c => c.MACHUHO, v => v.MAGIAODAN,
                (c, v) => v).FirstOrDefault() == null;
            return result;
        }

        public static bool Check_Update_HOGIADINH_GIAODAN(HOGIADINH_GIAODAN hogiadinhGiaoDan)
        {
            return true;
        }

        public static void Delete(HOGIADINH hogiadinh)
        {
            foreach (HOGIADINH_GIAODAN hogiadinhGiaodan in hogiadinh.REF_HOGIADINH_GIAODAN)
            {
                hogiadinhGiaodan.DeletebyId();
            }
        }

        public static void Delete(GIAODAN giaodan)
        {
            HOGIADINH_GIAODAN hogiadinhGiaodan = new HOGIADINH_GIAODAN();
            hogiadinhGiaodan.MAGIAODAN = giaodan.MAGIAODAN;
            hogiadinhGiaodan.Select();
            if (hogiadinhGiaodan.IsPersistence)
                hogiadinhGiaodan.DeletebyId();
        }
    }
}
